package com.eos.control.action.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eos.business.service.SysProductBusinessService;
import com.eos.common.tool.ConstantTool;
import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.ProductBrand;
import com.eos.orm.mybatis.model.SysCustomer;
import com.eos.orm.mybatis.model.SysProduct;

public class AddBillOfSignAction extends AbstractBaseAction{
	private static final long serialVersionUID = 82821536816308316L;
	private List<SysProduct> productList;
	private String brandOfProductJson;
	private List<SysCustomer> customerList;
	private Integer customerType;
	
	public List<SysProduct> getProductList() {
		return productList;
	}

	public String getBrandOfProductJson() {
		return brandOfProductJson;
	}

	public List<SysCustomer> getCustomerList() {
		return customerList;
	}

	public Integer getCustomerType() {
		return customerType;
	}

	@Override
	public String execute() {
		 try {
			 SysProductBusinessService spbs = new SysProductBusinessService();
				SysProduct product = new SysProduct();
				product.setDealFlag(ConstantTool.DEAL_FALG_OK);
				product.setStatus(ConstantTool.STATUS_OK);
				productList = spbs.getSysProductExample(product);
				ProductBrand brand = new ProductBrand();
				brand.setDealFlag(ConstantTool.DEAL_FALG_OK);
				brand.setStatus(ConstantTool.STATUS_OK);
				List<ProductBrand> brandList = spbs.getProductBrandExample(brand);
				brandOfProductJson= getProductBrandJson(productList, brandList);
				
				customerType = 0;
				List<SysCustomer> suList = DataCacheUtil.getAllSysCustomer();
				if(null != suList){
					List<SysCustomer> nsu = new ArrayList<SysCustomer>();
					for(SysCustomer su : suList){
						if("0".equals(su.getCustomerType())){// 0结算客户;1供应商;2融信
							nsu.add(su);
						}
					}
					customerList = nsu;
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	private String getProductBrandJson(List<SysProduct> proList, List<ProductBrand> brandList) {
		if (null != proList && proList.size() > 0 && null != brandList && brandList.size()>0) {
			Map<String, List<ProductBrand>> map = new HashMap<String, List<ProductBrand>>();
			for (SysProduct pro : proList) {
				for (ProductBrand brand : brandList) {
					int proId = pro.getId();
					if (brand.getProductId() == proId) {
						if (map.containsKey("pro_" + proId)) {
							map.get("pro_" + proId).add(brand);
						} else {
							List<ProductBrand> bList = new ArrayList<ProductBrand>();
							bList.add(brand);
							map.put("pro_"+proId, bList);
						}
					}
				}
			}
			return ObjectUtils.mapToJson(map);
		}
		return "";
	}
}
